package com.icebergteam.warfacewidget;

import com.icebergteam.warfacewidget.core.Database;
import com.icebergteam.warfacewidget.core.LocaleManager;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.MainTimer;
import com.icebergteam.warfacewidget.core.settings.FinderSettings;
import com.icebergteam.warfacewidget.core.settings.GeneralSettings;
import com.icebergteam.warfacewidget.core.settings.NewsSettings;
import com.icebergteam.warfacewidget.core.settings.Top100Settings;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.IBinder;

public class AppManager extends Service {
	public static final String className = AppManager.class.getCanonicalName();
	
	private static Context msAppContext;

	// Managers
	private static LocaleManager msLocaleManager;
	// Settings
	private static GeneralSettings	msGeneralSettings;
	private static NewsSettings		msNewsSettings;
	private static FinderSettings	msFinderSettings;
	private static Top100Settings	msTop100Settings;
	// 
	private static Database msDatabase;
	
	
	

	public static Context getContext() {
		return msAppContext;
	}

	public static void Init(Context context) throws Exception {
		msAppContext = context;
		if (getGeneralSettings().isAutodetectRegion())
			getLocaleManager().setupServerLocaleSettings();

		MainTimer.runTimer();
		getLocaleManager().updateLocale();
		
		msDatabase = new Database(msAppContext);
		
	}

	
	public static LocaleManager getLocaleManager() throws Exception {
		if (msLocaleManager == null)
			synchronized (AppManager.class) {
				if (msLocaleManager == null)
					msLocaleManager = new LocaleManager();
			}
		return msLocaleManager;
	}

	// Settings
	public static GeneralSettings getGeneralSettings() throws Exception {
		if (msGeneralSettings == null)
			synchronized (AppManager.class) {
				if (msGeneralSettings == null)
					msGeneralSettings = new GeneralSettings();
			}
		return msGeneralSettings;
	}

	public static NewsSettings getNewsSettings() {
		if (msNewsSettings == null)
			synchronized (AppManager.class) {
				if (msNewsSettings == null)
					msNewsSettings = new NewsSettings();
			}
		return msNewsSettings;
	}
	
	public static FinderSettings getFinderSettings() {
		if (msFinderSettings == null)
			synchronized (AppManager.class) {
				if (msFinderSettings == null)
					msFinderSettings = new FinderSettings();
			}
		return msFinderSettings;
	}
	
	public static Top100Settings getTop100Settings() {
		if (msTop100Settings == null)
			synchronized (AppManager.class) {
				if (msTop100Settings == null)
					msTop100Settings = new Top100Settings();
			}
		return msTop100Settings;
	}
	
	public static Database getDatabase() {
		return msDatabase;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		try {
			getLocaleManager().updateLocale();
			if (getGeneralSettings().isAutodetectRegion())
				getLocaleManager().setupServerLocaleSettings();
		} catch (Exception e) {
			LogManager.LogE(AppManager.className, LogManager.getExecuteMethodName(),
					e.getMessage());
			e.printStackTrace();
		}
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		 return START_STICKY;
	}

	
}